package com.xh.bussiness.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.bean.request.AddStudentResultRecordRequest;
import com.xh.bussiness.system.dao.StudentResultRecordMapper;
import com.xh.bussiness.system.model.Student;
import com.xh.bussiness.system.model.StudentResultRecord;
import com.xh.bussiness.system.service.IStudentResultRecordService;
import com.xh.bussiness.system.service.IStudentService;
import com.xh.core.bean.XhPage;
import com.xh.core.exception.XhException;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *
 * </p>
 *
 * @author Pitt zhang
 * @since 2020/5/12
 */
@Service
public class StudentResultRecordServiceImpl extends BaseServiceImpl<StudentResultRecordMapper, StudentResultRecord> implements IStudentResultRecordService {

    @Autowired
    private IStudentService studentService;

    @Override
    public XhPage<StudentResultRecord> queryBylwStudentId(XhPage page, String lwStudentId,String contractId) {
        Student student = studentService.queryStudentByLwStudentId(lwStudentId);
        if (student == null) {
            return new XhPage<>();
        }
        QueryWrapper<StudentResultRecord> qw = new QueryWrapper<>();
        qw.eq("student_id", student.getId());
        qw.eq("contract_id", contractId);
        qw.orderByDesc("create_time");
        return (XhPage<StudentResultRecord>) this.dao.selectPage(page, qw);
    }

    @Override
    public StudentResultRecord addStudentResultRecord(AddStudentResultRecordRequest resultRecordRequest) {
        Student student = studentService.queryStudentByLwStudentId(resultRecordRequest.getStudentId());
        if (student == null) {
            throw new XhException("学生不存在");
        }
        String msg = resultRecordRequest.check();
        if (!StringUtils.isEmpty(msg)) {
            throw new XhException(msg);
        }
        StudentResultRecord resultRecord;
        if (StringUtils.isEmpty(resultRecordRequest.getId())) {
            resultRecord = new StudentResultRecord();
            resultRecord.setContractId(resultRecordRequest.getContractId());
            resultRecord.setStudentId(student.getId());
            resultRecord.setStudentName(resultRecordRequest.getStudentName());
            resultRecord.setResultCategory(resultRecordRequest.getResultCategory());
            resultRecord.setTotalScore(resultRecordRequest.getTotalScore());
            resultRecord.setScore(resultRecordRequest.getScore());
            this.insert(resultRecord);
        } else {
            resultRecord = this.getById(resultRecordRequest.getId());
            resultRecord.setResultCategory(resultRecordRequest.getResultCategory());
            resultRecord.setTotalScore(resultRecordRequest.getTotalScore());
            resultRecord.setScore(resultRecordRequest.getScore());
            this.update(resultRecord);
        }
        return resultRecord;
    }

}
